<?php
/**
 * User: hjd-yao
 * Date: 2019/4/30
 * Time: 17:51
 */

namespace Frontend\Models;

use App\Helpers\SelfEncrypt;
use App\Helpers\SelfValidationRule;
use DC\UserCenter\Models\UserAuthModel;
use DC\UserCenter\Models\UserBaseModel as UserModel;
use Illuminate\Support\Facades\DB;

class UserBaseModel extends UserModel
{
    public function login($user_name, $login_pwd,$field='user_number')
    {
        if(SelfValidationRule::mobileFormat($user_name)){
            $field = 'user_phone';
        }

        $info = self::with(['auth'=>function($query){
            $query->select(['login_pwd','login_salt']);
        }])->where($field,$user_name)->first();

        if(!$info){
            $this->errorMsg = '登录账号不存在!';
            return false;
        }

        if($info->status<0){
            $this->errorMsg = '账号未激活或已被冻结!';
            return false;
        }

        if(!UserAuthModel::init()->checkLoginPwd($login_pwd,$info->member_id)){
            $this->errorMsg = '账号或密码错误!';
            return false;
        }
        $this->autoLogin([
            'member_id'=>$info->member_id,
            'user_number'=>$info->user_number,
            'login_count'=>$info->login_count,
            'real_name'=>$info->real_name,
        ]);
        return true;
    }


    // 后台登录前台

    public function adminLogin($user_name, $login_pwd,$field='user_number')
    {
        $info = self::with(['auth'=>function($query){
            $query->select(['login_pwd','login_salt']);
        }])->where($field,$user_name)->first();

        if(!$info||$info->status<0){
            $this->errorMsg = '账号或密码错误!';
            return false;
        }
        //记录日志
        $this->autoLogin([
            'member_id'=>$info->member_id,
            'user_number'=>$info->user_number,
            'login_count'=>$info->login_count,
            'real_name'=>$info->real_name,
        ]);
        return true;
    }

    public function autoLogin($user)
    {
        $auth = $update_data = [
            'login_at'=>request()->server('REQUEST_TIME'),
            'login_ip'=>get_client_ip(1),
            'login_count'=>DB::raw('`login_count`+1'),
            'web_token'=>SelfEncrypt::createToken(24,$user['user_number'])
        ];
        UserAuthModel::where('member_id', $user['member_id'])->update($update_data);

        $auth['member_id'] = $user['member_id'];
        $auth['user_number'] = $user['user_number'];
        $auth['real_name'] = $user['real_name'];
        $auth['login_count'] = $user['login_count']+1;

        $user_session['lg_info'] = $auth;
        $user_session['lg_info_sign'] = SelfEncrypt::dataAuthSign($auth);
        request()->session()->put('user',$user_session);
        return true;
    }

    public function logout()
    {
        return request()->session()->forget('user');
    }

    public function isLogin()
    {
        if(request()->session()->has('user')){
            $user_auth = request()->session()->get('user');
            if($user_auth['lg_info_sign'] == SelfEncrypt::dataAuthSign($user_auth['lg_info'])){
                return true;
            }
        }
        return false;
    }

    public function isOtherLogin()
    {
        if(request()->session()->has('user')){
            $user_auth = request()->session()->get('user');
            $web_token = UserAuthModel::where('member_id', $user_auth['lg_info']['member_id'])->value('web_token');
            if($web_token == $user_auth['lg_info']['web_token']){
                return true;
            }
        }
        return false;
    }

    public function loginInfo(string $field = null)
    {
        $login_info = request()->session()->get('user');
        $info = $login_info['lg_info'];
        if(!$field){
            return $info;
        }

        if(isset($info[$field])){
            return $info[$field];
        }
        return false;
    }
}